package com.xwqkwl.aimin.auth.controller;

import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import com.xwqkwl.aimin.auth.common.response.Result;
import com.xwqkwl.aimin.auth.pojo.jscode2sessionResult;
import com.xwqkwl.aimin.auth.service.WxService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/public/wx")
@RequiredArgsConstructor
public class WxController {

    private final WxService wxService;

    /**
     * 微信登录
     * @param code 微信登录返回的code
     * @return 微信登录结果
     */
    @RequestMapping("/token")
    public Result<?> token(String code) {
        jscode2sessionResult jscode2sessionResult = wxService.WxLogin(code);
        //保存数据量
        StpUtil.login("100001");
        SaTokenInfo tokenInfo = StpUtil.getTokenInfo();
        String tokenName = tokenInfo.getTokenName();
        String tokenValue = tokenInfo.getTokenValue();
        if (tokenName == null || tokenValue == null){
            return Result.error("登录失败");
        }
        return Result.success(tokenInfo);
    }

}
